package utils.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Tools {
	private static final String username = "root";
	private static final String password = "etoiles86";
	private static Connection connection = null;
	private static Statement stat = null;
	
	public static ResultSet executeSelect(String dbName, String sQuery){
		ResultSet results = null;
		try
		{
			stat = connectToDb(dbName);
			results = stat.executeQuery(sQuery);
			
			
		}
		catch(Exception E){ 
			System.err.println(E.getMessage());	
		}
		return results;
	}
	
	public static Statement connectToDb(String dbName) throws Exception{
		Class.forName("com.mysql.jdbc.Driver").newInstance(); 
		String dbURL = String.format("jdbc:mysql://localhost/%s?user=%s&password=%s",dbName,username,password);

		connection = DriverManager.getConnection(dbURL);
		stat = connection.createStatement();
		return stat;
	}
	
	public static void closeAll(){
		try {
			stat.close();
			connection.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
